<?php
/**
 * Created by PhpStorm.
 * User: maple
 * Date: 17-11-28
 * Time: 下午3:51
 */

namespace AppBundle\Services;


use Doctrine\DBAL\Driver\IBMDB2\DB2Connection;
use AppBundle\Constants\Constant;

class HandleConfigSerivice extends BaseService
{
    /** @var  DB2Connection */
    private $dbconn;

    /**
     * HandleConfigSerivice constructor.
     * @param $dbconn
     */
    public function __construct($dbconn)
    {
        $this->dbconn = $dbconn;
    }

    /**
     * @param $code
     * @return array|null
     */
    public function getHandleConfigByCode($code)
    {
        try {
            $sql = 'SELECT * FROM CX_INTF_DF WHERE INTERFACE_NUM=?';
            $sth = $this->dbconn->prepare($sql);
            $sth->bindValue(1, $code);
            $sth->execute();
            $result = $sth->fetchAll();
            if (empty($result)) {
                return null;
            }
            return $result[0];
        } catch (\Exception $ex) {
            $this->logError('[HandleConfigSerivice getHandleConfigByCode] has exception, message: ' . $ex->getMessage());
            throw $ex;
        }
    }

    /**
     * @param $configCode
     * @param $paramType
     * @return array
     */
    public function getHandleConfigParamsByConfigCode($configCode, $paramType)
    {
        $sql = 'SELECT * FROM CX_PARM_SET WHERE INTF_ID=? and PARAM_TYPE=?';
        $sth = $this->dbconn->prepare($sql);
        $sth->bindValue(1, $configCode);
        $sth->bindValue(2, $paramType);
        $sth->execute();
        $result = $sth->fetchAll();
        return $result;
    }

    /**
     * @param $configCode
     * @return array
     */
    public function getWhereParamsByConfigCode($configCode)
    {
        return $this->getHandleConfigParamsByConfigCode(
            $configCode,
            Constant::HANDLE_PARAM_TYPE_WHERE
        );
    }

    /**
     * @param $configCode
     * @return array
     */
    public function getOrderParamsByConfigCode($configCode)
    {
        return $this->getHandleConfigParamsByConfigCode(
            $configCode,
            Constant::HANDLE_PARAM_TYPE_ORDER
        );
    }
}